
/*-----------------------------------------------------------------------------------*/
/* Nuvoton Technology Corporation confidential                                       */
/*                                                                                   */
/* Copyright (c) 2008 by Nuvoton Technology Corporation                              */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/* File Contents:                                                                                          */
/*   rtc_demo.c_                                                                                           */
/*            This file contains                                                                           */
/* Project:                                                                                                */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of system headers                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "nuc900_rtc.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of local headers                                                                               */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
#define printf sysprintf

/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
INT i=0;
INT RTC_FD;
INT nRet;
CHAR Week[7][10] = {"Sunday   ",
                    "Monday   ",
                    "Tuesday  ",
                    "Wednesday",
                    "Thursday ",
                    "Friday   ",
                    "Saturday " };

RTC_TIME_DATA_T td;
RTC_TICK_T tkd;

BOOL volatile IsAlarmCallBack=0;
BOOL volatile IsTickCallBack=0;
BOOL volatile IsWakeUpCallBack=0;

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <DisplayRTCTime>                                                                              */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*              Show RTC current time                                                                      */
/*---------------------------------------------------------------------------------------------------------*/

VOID DisplayRTCTime(VOID)
{
	INT nRet;

    memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    
    nRet = RTC_Read(CURRENT_TIME,&td);
    
    if (nRet == 0)
    {
        if (td.ucClockDisplay == RTC_CLOCK_12)                            /* 12 hour format                */
        {
            printf("%d.%02d.%d %s %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond,
                       (td.ucAmPm == RTC_AM) ? "AM" : "PM");
        }
        else                                                             /* 24 hour format                 */
        {
            printf("%d.%02d.%d %s %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond);
        }
    }
    else
    {
        printf("\n Get rtc time data fail [%x]\n", nRet);
    }    
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <DisplayAlarmTime>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*              Read RTC Alarm Time Setting                                                                */
/*---------------------------------------------------------------------------------------------------------*/

VOID DisplayAlarmTime(VOID)
{
    INT nRet;

    memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    nRet = RTC_Read(ALARM_TIME, &td);
    
    
    if (nRet == 0)
    {
        if ( (td.ucAmPm == RTC_AM) || (td.ucAmPm == RTC_PM) )
        {
            printf("%d.%02d.%d %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       td.ucHour, td.ucMinute, td.ucSecond, (td.ucAmPm == RTC_AM) ? "AM" : "PM");
        }
        else                                                            /* 24 hour format                  */
        {
            printf("%d.%02d.%d %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       td.ucHour, td.ucMinute, td.ucSecond);
        }
    }
    else
    {
        printf("\n Get rtc time data fail [%x]\n", nRet);
    }    
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <AlarmCallBackDemo>                                                                           */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
VOID AlarmCallBackDemo(VOID)
{
    
    printf(" Alarm OK\n");
    IsAlarmCallBack=1;
 
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <TickCallBackDemo>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID TickCallBackDemo(VOID)
{
 	printf("Tick INT OK \n");   
    IsTickCallBack=1;
   
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <WakeUpCallBackDemo>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID WakeupCallBackDemo()
{
	printf("Wake up OK \n");
	IsWakeUpCallBack=1;

}


/*---------------------------------------------------------------------------------------------------------*/
/* Function: <RTC_SetTime>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID RTC_SetTime()
{
	   

      td.ucClockDisplay = RTC_CLOCK_12;  /* Set 12-hour mode have to set ucAmPm                  */
      td.ucAmPm = RTC_AM;

      td.uYear = 2009;
      td.ucMonth = 1;
      td.ucDay = 18;
      td.ucDayOfWeek = WEDNESDAY;
      td.ucHour = 11;
      td.ucMinute = 59;
      td.ucSecond = 55;
      nRet = RTC_Write(CURRENT_TIME, &td);
      
      printf("Set RTC Current time data ...");
      if (nRet == 0)
      {
          
          printf("OK \n");
      }    
      else
      {
          printf("Fail\n");
	  }
      
      td.ucClockDisplay = RTC_CLOCK_12;  /* Set 12-hour mode have to set ucAmPm                  */
      td.ucAmPm = RTC_AM;

      td.uYear = 2008;
      td.ucMonth = 2;
      td.ucDay = 22;
      td.ucDayOfWeek = WEDNESDAY;
      td.ucHour = 03;
      td.ucMinute = 02;
      td.ucSecond = 01;
      nRet = RTC_Write(ALARM_TIME, &td);
      printf("Set RTC Alarm time data .....");
      if (nRet == 0)
      {
      	  
          printf("OK \n");
      }    
      else
      {
          printf("Fail\n");
	  }
	  

}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <RTC_Alarm_INT>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/


VOID RTC_Alarm_INT()
{
    UINT32 btime;
    
    printf("RTC Alarm Test........");
    
    td.ucClockDisplay = RTC_CLOCK_12;   /* Be sure to set RTC first.                   */
    td.ucAmPm         = RTC_AM;
    td.uYear          = 2009;
    td.ucMonth        = 5;
    td.ucDay          = 6;
    
    td.ucDayOfWeek    = WEDNESDAY;
    td.ucHour         = 5;
    td.ucMinute       = 0;
    td.ucSecond       = 0;
    
    RTC_Write(CURRENT_TIME,&td);
    
    
    td.ucAmPm = RTC_AM;  /* if set RTC as 12-hr mode, should set ucAmPm for alarm      */
    td.uYear          = 2009;
    td.ucMonth        = 5;
    td.ucDay          = 6;
    
    td.ucHour         = 5;
    td.ucMinute       = 0;
    td.ucSecond       = 5;
    td.fnAlarmCallBack = AlarmCallBackDemo;
    
    RTC_Write(ALARM_TIME, &td);
    
    RTC_Ioctl(RTC_FD,RTC_IOC_ENABLE_INT, ALARM_INT, 0); 
    
    printf(" wait 5 sec ");
    
    sysStartTimer(TIMER0, 1000, PERIODIC_MODE);
	
	btime = sysGetTicks(TIMER0);
	
	while(IsAlarmCallBack==0)
	{
  	 
	   	if(sysGetTicks(TIMER0) - btime >=1000)
	   	{
	   	
	   		btime = sysGetTicks(TIMER0);
	   		
	   		printf(".");
	   	}
	   
	}
	
    
  
       
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <RTC_Tick_INT>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID RTC_Tick_INT()
{
	tkd.ucMode = RTC_TICK_1_SEC;    
    
    tkd.fnTickCallBack = TickCallBackDemo;               
    
    printf("RTC Tick INT Test......");
    
    RTC_Ioctl(RTC_FD, RTC_IOC_SET_TICK_MODE, (UINT32)&tkd, 0);
    
    
    RTC_Ioctl(RTC_FD,RTC_IOC_ENABLE_INT, TICK_INT, 0);   /* Enable tick interrupt  */
     
    while(IsTickCallBack==0);
    
    RTC_Ioctl(RTC_FD,RTC_IOC_DISABLE_INT, TICK_INT, 0);   /* Disable tick interrupt  */
  
  	tkd.fnTickCallBack = NULL;                            //Destroy Callback Function
}


/*---------------------------------------------------------------------------------------------------------*/
/* Function: <RTC_WakeUp_INT>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID RTC_WakeUp_INT()
{
       
    printf("RTC Wake up test.......");
    
    td.ucClockDisplay = RTC_CLOCK_24;            /* Set Loading & Alarm Time Data              */
    td.uYear          = 2007;
    td.ucMonth        = 05;
    td.ucDay          = 16;
    td.ucDayOfWeek    = WEDNESDAY;
    td.ucHour         = 9;
    td.ucMinute       = 3;
    td.ucSecond       = 0;
    RTC_Write(CURRENT_TIME, &td);

    td.uYear          = 2007;
    td.ucMonth        = 05;
    td.ucDay          = 16;
    td.ucHour         = 9;
    td.ucMinute       = 3;
    td.ucSecond       = 5;
    td.fnAlarmCallBack = WakeupCallBackDemo;
    
    RTC_Write(ALARM_TIME, &td);
    
    printf("wait 5 sec ");
     
    RTC_Ioctl(RTC_FD,RTC_IOC_ENABLE_INT, WAKEUP_INT, 0); 
   
    outpw(REG_PMCON,inpw(REG_PMCON)&2);          /* Entry Power Down Mode                    */

	while(IsWakeUpCallBack==0);
	
	
             
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <RTC_ReadTest>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/


VOID RTC_ReadTest()
{
	INT nRet,count=0,btime=0;
	
	printf("Get RTC Time Test......\t\t\n");
	
	do{    
    	memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    
    	nRet = RTC_Read(CURRENT_TIME,&td);
    
    	if (nRet == 0)
    	{
    
        	if (td.ucClockDisplay == RTC_CLOCK_12)                            /* 12 hour format                */
        	{
            	printf("\t %d.%02d.%d %s %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond,
                       (td.ucAmPm == RTC_AM) ? "AM" : "PM");
       	 	
       	 	}
        	else                                                              /* 24 hour format                 */
        	{
            	printf("\t %d.%02d.%d %s %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond);
        	}
        	count++;
    	}
    	else
    	{
        	printf("\n Get rtc time data fail [%x]\n", nRet);
        	
    	}
     	    
    	sysStartTimer(TIMER0, 1000, PERIODIC_MODE);
		btime = sysGetTicks(TIMER0);
		while(1)
		{
	    	if(sysGetTicks(TIMER0) - btime >=1000)
	    	{
	    		break;
	    	}
	    
	    }
	
    	
	}while(count<10);
	
	printf(".............................OK \n");

}


VOID RTC_GetTickTest(INT sec)
{
	
	UINT count,btime;
	
	tkd.ucMode = RTC_TICK_1_SEC;    
    
    RTC_Ioctl(RTC_FD, RTC_IOC_SET_TICK_MODE, (UINT32)&tkd, 0);
	
	RTC_Ioctl(RTC_FD, RTC_IOC_ENABLE_INT, TICK_INT, 0);   /* Enable tick interrupt  */

	printf("Get Tick Test....wait %d sec ",sec);
	
    count = RTC_Ioctl (RTC_FD, RTC_IOC_GET_TICK, 0, 0);
	
	btime = RTC_Ioctl (RTC_FD, RTC_IOC_GET_TICK, 0, 0);
	    	
    while(1)
    {
	   	
	   	if(RTC_Ioctl(RTC_FD, RTC_IOC_GET_TICK, 0, 0) - btime >=1)
	  	{
			    
			btime = RTC_Ioctl (RTC_FD, RTC_IOC_GET_TICK, 0, 0);
			printf(".");
    	}
	   	
	   	
	   	if(RTC_Ioctl(RTC_FD, RTC_IOC_GET_TICK, 0, 0) - count >=sec)
	  	{
	    	printf("OK\n\n");
			break;

    	}
    }


	RTC_Ioctl(RTC_FD, RTC_IOC_DISABLE_INT, TICK_INT, 0);   /*Disable tick interrupt  */





}



/*---------------------------------------------------------------------------------------------------------*/
/* Function:    <Main>                                                                                     */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              0    Successful                                                                            */
/* Description:                                                                                            */
/*              Main Function                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
INT main(VOID)
{
    WB_UART_T	my_uart;    
  
    /* UART setting */

	my_uart.uiFreq     = 15000000; /* 15MHZ */
	my_uart.uiBaudrate = 115200;
	my_uart.uiDataBits = WB_DATA_BITS_8;
	my_uart.uiStopBits = WB_STOP_BITS_1;
	my_uart.uiParity   = WB_PARITY_NONE;
	my_uart.uiRxTriggerLevel = LEVEL_1_BYTE;
	
	sysInitializeUART(&my_uart);
	   
    nRet = RTC_Init();
    
    if (nRet != 0)
    {
        printf("FAILED \n");
        return 0;
    }
    else
    {
   	    printf("\n\nRTC INIR.....................OK \n");
    }
    RTC_SetTime();
    RTC_ReadTest();
    RTC_Alarm_INT();
    RTC_Tick_INT();
    RTC_WakeUp_INT();
    RTC_GetTickTest(5);


 	RTC_Close();  
    return 0;
}
